//
//  NSStringCategories.swift
//  SwiftKit
//
//  Created by LL on 2023/4/7.
//

import Foundation

public extension NSString {
    var isEmpty: Bool { self.length == 0 }
    
    var rangeOfAll: NSRange { NSRange(location: 0, length: self.length) }
    
    /// 系统方法`substring(with:NSRange)`在NSRange越界的情况下会导致异常，该方法在越界的情况下只会返回nil。
    func lilSubstring(with range: NSRange?) -> NSString? {
        guard let range = range else { return nil }
        if range.location < 0 || range.length < 0  { return nil }
        if NSMaxRange(range) > self.length { return nil }
        return self.substring(with: range) as NSString
    }
}
